local ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. "ExternalFun")
local HeadSprite = appdf.req(appdf.EXTERNAL_SRC .. "HeadSprite")
local MultiPlatform = appdf.req(appdf.EXTERNAL_SRC .. "MultiPlatform")

local GameRoleInfo = class("GameRoleInfo", function()
	local layer = CCLayerColor:create(cc.c4b(0, 0, 0, 0))--cc.Layer:create()
	return layer
end)
local this = nil

function GameRoleInfo:ctor(viewParent,userItem,tbPostion,rootParent)
	this = self
    self.m_parent = viewParent
    self:addTo(self.m_parent)
	self._rootParent = rootParent or viewParent

	--加载csb资源
    local csbNode = ExternalFun.loadCSB("GameRoleInfo/GameRoleInfo.csb", self)
    csbNode:setPosition(tbPostion)
    self.m_csbNode = csbNode


    local bg_kuang = csbNode:getChildByName("bg_kuang")
    local head_bg = bg_kuang:getChildByName("head_bg")
    
    local head = HeadSprite:create(HeadSprite.NORMAL,userItem, 140)
    if nil ~= head then
        head:setAnchorPoint(0.5,0.5)
        -- head:setPosition(self.m_headPos["x"], self.m_headPos["y"])
        head_bg:addChild(head)
    end
	
	
    local role_name = bg_kuang:getChildByName("nick_text")
    local role_gold = bg_kuang:getChildByName("role_gold")
    role_gold:setString(ExternalFun.ChangeScore(userItem.lScore,2,""))
    ExternalFun.transName( userItem.szNickName,role_name)
	
    --按钮回调
	local btcallback = function (ref, type)
		
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
			if this._isGameCD == true then
				showToast(self,"表情冷却中",1)
				return
			end
			 local tag =  ref:getTag() 
			--[[
            appdf.PostCustomEvent("biaoqingEvent",{dwUserID=userItem.dwUserID,
            wChairID = userItem.wChairID,aniType = tag},self)
            ]]
			print(" GameRoleInfo:ctor --------------------- nil")
			if self._rootParent  then
					print(" GameRoleInfo:ctor ---------------------")
					self._rootParent:sendBiaoQingAni(userItem.dwUserID,userItem.wChairID,tag)
			end
			
			--添加CD
			self:ResetGameWaitTime()
			this:removeSelf()
			this = nil
		end
    end
    
	local timerPosition = {cc.p(61,49),cc.p(158,49),cc.p(254,49),cc.p(351.5,49)}
    for i=1,4 do
        local btn = bg_kuang:getChildByName("biaoqing_btn"..i)
        btn:setTag(i)
        btn:addTouchEventListener(btcallback)
		
		--添加倒计时
		local timer = cc.ProgressTimer:create(cc.Sprite:create("GameRoleInfo/ptimer.png"))
		timer:setType(cc.PROGRESS_TIMER_TYPE_RADIAL)
		timer:setPosition(timerPosition[i])
		timer:setPercentage(100)
		timer:setTag(100+i)
		timer:setScaleX(-1)
		timer:addTo(bg_kuang)
    end
	
	--
	this._isGameCD = false
	if  yl.CHAT_WIAT_TIME >= 0 then
		 --添加倒计时
		local dNode = cc.Director:getInstance():getRunningScene():getChildByName("GameRoleInfoTime")
		if dNode then
			dNode:removeFromParent()
		end
		 
		this._isGameCD = true
		local node = cc.Node:create()
		cc.Director:getInstance():getRunningScene():addChild(node)
		schedule(node,function()
			yl.CHAT_WIAT_TIME = yl.CHAT_WIAT_TIME-1
			if yl.CHAT_WIAT_TIME > 0 then
				if this then
						this:UpdateTimer()
				end
			else	
				node:removeFromParent()
				yl.CHAT_WIAT_TIME = 0
				
				if this then
					this._isGameCD = false
					this:UpdateTimer()
				end
			end

		end,0)

	end
	self:UpdateTimer()	
	self:ToSpriteOutArea(self.m_csbNode,"bg_kuang",function(name,obj) 
			if name == "end" then
					this:removeSelf()
					this = nil
					return true
			end
	end)
end


--区域外响应
function GameRoleInfo:ToSpriteOutArea(parent, name, callback,attr)

    local _sprite = parent:getChildByName(name);--seekByName(parent, name);
	if not _sprite then
        error("ToSprite == nil ", name);
        return;
    end

    if callback then
        local onTouchBegin = function (touch, e) 
			local target = e:getCurrentTarget();--获取的当前触摸的目标
			local locationInNode = target:convertToNodeSpace(touch:getLocation());
			local  s = target:getContentSize();
			local  rect = cc.rect(0, 0, s.width, s.height);        
			if cc.rectContainsPoint(rect,locationInNode) then --判断触摸点是否在目标的范围内
				return false;
			else
				return true;
			end
		end
		
        local onTouchMove = function (touch, e)
		end
		
        local onTouchEnd = function (touch, e)
			performWithDelay(self,function() 
				callback("end",_sprite); 
			end,0.1)
		end
		
        local listent = cc.EventListenerTouchOneByOne:create();
		listent:setSwallowTouches ( false );--设置是否想下传递触摸  
		
        listent:registerScriptHandler(onTouchBegin,cc.Handler.EVENT_TOUCH_BEGAN);
        listent:registerScriptHandler(onTouchMove,cc.Handler.EVENT_TOUCH_MOVED);
        listent:registerScriptHandler(onTouchEnd,cc.Handler.EVENT_TOUCH_ENDED);
        local eventDispatcher = _sprite:getEventDispatcher();
        eventDispatcher:addEventListenerWithSceneGraphPriority(listent,_sprite);
    end
end


function GameRoleInfo:ResetGameWaitTime()
	yl.CHAT_WIAT_TIME = yl.CHAT_WIAT_MAX
	--启动倒计时
	local node = cc.Node:create()
	node:setName("GameRoleInfoTime")
	cc.Director:getInstance():getRunningScene():addChild(node)
	schedule(node,function()
		yl.CHAT_WIAT_TIME = yl.CHAT_WIAT_TIME-1
		if yl.CHAT_WIAT_TIME <= 0 then
			node:removeFromParent()
			yl.CHAT_WIAT_TIME =0
		end
	end,0)
end

function GameRoleInfo:UpdateTimer()
	local percent = (yl.CHAT_WIAT_TIME/yl.CHAT_WIAT_MAX) *100
	local bg_kuang = self.m_csbNode:getChildByName("bg_kuang")
	for i = 101,104 do
		local timer = bg_kuang:getChildByTag(i)
		if timer  then
			timer:setPercentage(percent)
		end
	end
end


return GameRoleInfo